package com.aaron.contentcenter.service;

import com.aaron.contentcenter.dao.content.ShareMapper;
import com.aaron.contentcenter.domain.dto.share.ShareDTO;
import com.aaron.contentcenter.domain.dto.user.UserDTO;
import com.aaron.contentcenter.domain.entity.content.Share;
import com.aaron.contentcenter.feignclient.UserCenterFeignClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 *
 * @author linzhj
 * @date 2021/7/7 23:21
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class ShareService {

    private final ShareMapper shareMapper;

//    @Autowired
//    private RestTemplate restTemplate;
//
//    @Autowired
//    private DiscoveryClient discoveryClient;

    @Autowired
    private UserCenterFeignClient userCenterFeignClient;

    public ShareDTO findById(Integer id){
        Share share = this.shareMapper.selectByPrimaryKey(id);

        //获取用户中心的所有实例
//        List<ServiceInstance> instances= discoveryClient.getInstances("user-center");
//        for(String services : discoveryClient.getServices()){
//            log.info("注册了服务：{}", services);
//        }

//        String targetUrl = instances.stream().map(instance -> instance.getUri().toASCIIString() + "/users/{id}")
//                .findFirst().orElseThrow(() -> new IllegalArgumentException("实例不存在"));

//        UserDTO userDTO = this.restTemplate.getForObject(
//                "http://user-center/users/{userid}"
//                , UserDTO.class , share.getUserId());

        UserDTO userDTO = userCenterFeignClient.findById(share.getUserId());
        //消息装配
        ShareDTO shareDTO = new ShareDTO();

        BeanUtils.copyProperties(share, shareDTO);

        shareDTO.setWxNickname(userDTO.getWxNickname());

        return shareDTO;
    }


}
